package com.popo.serializable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;


public class ExtenlizableTest  {
	
	public static void main(String[] args) {
		B1 b = new B1();
		byte[] bytes = null;
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		
		try {
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(b);
			oo.close();
			bytes=bo.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			bo.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
		System.out.println( "===========================");
		try {
			ObjectInputStream oi = new ObjectInputStream(bi);
			try {
				B1 b_= (B1) oi.readObject();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		B2 b2 = new B2();
		
		byte[] bytes2 = null;
		ByteArrayOutputStream bo2 = new ByteArrayOutputStream();
		
		try {
			ObjectOutputStream oo2 = new ObjectOutputStream(bo2);
			oo2.writeObject(b2);
			oo2.close();
			bytes=bo.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			bo.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ByteArrayInputStream bi2 = new ByteArrayInputStream(bytes2);
		System.out.println( "===========================");
		try {
			ObjectInputStream oi2 = new ObjectInputStream(bi2);
			try {
				B2 b_2= (B2) oi2.readObject();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}

class B1 implements Externalizable{
	
	private String b1 = "B1";
	
	public B1(){
		System.out.println("B1");
	}
	
	public String  getB1(){
		return b1;
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("writeExternal");
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		// TODO Auto-generated method stub
		System.out.println("readExternal");
		
	}
	
}


class B2 implements Externalizable{
	public B2(){
		System.out.println("B2");
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("writeExternal");
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		// TODO Auto-generated method stub
		System.out.println("readExternal");
		
	}
}
